package com.javasee.rain.stock.service;

import com.javasee.rain.stock.mapper.BoardMapper;
import com.javasee.rain.stock.model.Board;
import com.javasee.rain.stock.model.BoardStock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 板块数据服务类
 * 
 * @author : xinyanfei
 * @date : 2025/10/10
 * Copyright (c) 2025, 京东商城  All Rights Reserved.
 */
@RestController
@RequestMapping("/api/board")
@CrossOrigin(origins = "*") // 允许跨域访问
public class BoardService {

    @Autowired
    private BoardMapper boardMapper;

    /**
     * 获取最近10天的板块轮动数据
     */
    @GetMapping("/rotation")
    public ResponseEntity<Map<String, Object>> getRecentBoardRotation() {
        try {

            String concatenatedDates = boardMapper.selectDateList();
            System.out.println("concatenatedDates = " + concatenatedDates);
            
            if (concatenatedDates == null || concatenatedDates.trim().isEmpty()) {
                System.out.println("没有获取到日期数据");
                Map<String, Object> errorResponse = new HashMap<>();
                errorResponse.put("code", 404);
                errorResponse.put("message", "没有获取到日期数据");
                errorResponse.put("data", null);
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
            }
            
            String[] dates = concatenatedDates.split(",");
            System.out.println("dates = " + java.util.Arrays.toString(dates));

            List<Board> boardList = boardMapper.selectRecentBoardData(dates);

            for(Board board : boardList){
                System.out.println("board = " + board);
            }



            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "成功");
            response.put("data", boardList);

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            System.err.println("获取板块轮动数据失败: " + e.getMessage());
            e.printStackTrace();
            
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "服务器错误: " + e.getMessage());
            errorResponse.put("data", null);

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(errorResponse);
        }
    }

    /**
     * 根据日期获取板块数据
     */
    @GetMapping("/{date}")
    public ResponseEntity<List<Board>> getBoardByDate(@PathVariable String date) {
        try {
            System.out.println("查询板块数据，日期: " + date);
            
            if(!StringUtils.isEmpty(date)){
                date = date.replace("-", "");
            }
            System.out.println("查询板块数据，日期replace: " + date);
            
            List<Board> boardList = boardMapper.selectByDate(date);
            return ResponseEntity.ok(boardList);
        } catch (Exception e) {
            System.err.println("查询板块数据失败: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 根据板块代码获取最近10天的数据
     */
    @GetMapping("/code/{bkCode}")
    public ResponseEntity<List<Board>> getBoardByBkCode(@PathVariable String bkCode) {
        try {
            System.out.println("查询板块历史数据，板块代码: " + bkCode);
            List<Board> boardList = boardMapper.selectByBkCode(bkCode);
            return ResponseEntity.ok(boardList);
        } catch (Exception e) {
            System.err.println("查询板块历史数据失败: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 根据板块名称获取最近10天的数据
     */
    @GetMapping("/name/{bkName}")
    public ResponseEntity<List<Board>> getBoardByBkName(@PathVariable String bkName) {
        try {
            System.out.println("查询板块历史数据，板块名称: " + bkName);
            List<Board> boardList = boardMapper.selectByBkName(bkName);
            return ResponseEntity.ok(boardList);
        } catch (Exception e) {
            System.err.println("查询板块历史数据失败: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 根据日期和板块代码获取板块涨停明细
     */
    @GetMapping("/stocks/{date}/{bkCode}")
    public ResponseEntity<List<BoardStock>> getBoardStocksByDateAndCode(
            @PathVariable String date, 
            @PathVariable String bkCode) {
        try {
            System.out.println("查询板块涨停明细，日期: " + date + ", 板块代码: " + bkCode);
            
            if(!StringUtils.isEmpty(date)){
                date = date.replace("-", "");
            }
            
            List<BoardStock> boardStockList = boardMapper.selectBoardStocksByDateAndCode(date, bkCode);
            return ResponseEntity.ok(boardStockList);
        } catch (Exception e) {
            System.err.println("查询板块涨停明细失败: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}